package com.liwen.service.task;

import com.liwen.service.task.model.TaskParam;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.RecursiveAction;

/**
 * Created by g.l on 2018/4/19 0019.
 *
 * @Description: 没有返回结果的任务
 */
public class MultiTablesQueryTask extends RecursiveAction {

    private int suffix;
    private int count;
    private TaskParam taskParam;
    private MultiTablesQuery func;

    public MultiTablesQueryTask(Integer suffix, int count, TaskParam taskParam, MultiTablesQuery multiTablesQuery) {
        if (suffix != null) {
            this.suffix = suffix;
        }
        if (taskParam != null) {
            this.taskParam = taskParam;
        }
        this.count = count;
        this.func = multiTablesQuery;
    }

    @Override
    protected void compute() {
        if (count > 1) {
            invokeAll(createChildTasks());
        } else {
            func.query(suffix, taskParam);
        }
    }

    private List<MultiTablesQueryTask> createChildTasks() {
        List<MultiTablesQueryTask> list = new ArrayList<>(count);
        for (int i = 0; i < count; i++) {
            MultiTablesQueryTask task = new MultiTablesQueryTask(i, 0, taskParam, func);
            list.add(task);
        }
        return list;
    }

}
